package wre.data;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.supercsv.cellprocessor.ParseInt;
import org.supercsv.cellprocessor.constraint.StrMinMax;
import org.supercsv.cellprocessor.constraint.Unique;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.exception.SuperCSVReflectionException;
import org.supercsv.io.CsvBeanReader;
import org.supercsv.io.CsvBeanWriter;
import org.supercsv.io.ICsvBeanReader;
import org.supercsv.io.ICsvBeanWriter;
import org.supercsv.prefs.CsvPreference;

import wre.Retort;

public class RetortDAO {
	private Map<Integer, String> retorts;
	private final static CellProcessor[] processors = new CellProcessor[] {
			new Unique(new ParseInt()), new StrMinMax(1, 100) };
	private static final String FILENAME = "retort.csv";
	private static final String[] HEADER = new String[] { "id", "retort" };

	private RetortDAO() {

	}

	public static RetortDAO getInstance() {
		Map<Integer, String> retorts = new HashMap<Integer, String>();
		RetortDAO ret = new RetortDAO();
		try {
			ICsvBeanReader inFile = new CsvBeanReader(new FileReader(FILENAME),
					CsvPreference.EXCEL_PREFERENCE);
			try {
				final String[] header = inFile.getCSVHeader(true);
				Retort retort;
				while ((retort = inFile.read(Retort.class, header, processors)) != null) {
					retorts.put(retort.getId(), retort.getRetort());
				}
			} finally {
				inFile.close();
			}
			ret.setRetorts(retorts);
		} catch (SuperCSVReflectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return ret;

	}

	public void addRetort(String retort) {
		int id = retort.hashCode();
		retorts.put(id, retort);
	}

	public Map<Integer, String> getRetorts() {
		return retorts;
	}

	public void setRetorts(Map<Integer, String> retorts) {
		this.retorts = retorts;
	}

	public String getRetort(int retortId) {
		return retorts.get(retortId);
	}

	public static void main(String[] args) {
		RetortDAO dao = getInstance();
		dao.addRetort("I don't understand");
		dao.writeData();
	}

	public void writeData() {
		ICsvBeanWriter writer;
		try {
			writer = new CsvBeanWriter(new FileWriter(new File(FILENAME)),
					CsvPreference.EXCEL_PREFERENCE);

			try {

				// set up some data to write
				Iterator<Entry<Integer, String>> iterator = retorts.entrySet()
						.iterator();
				Entry<Integer, String> entry = null;
				writer.writeHeader(HEADER);
				while (iterator.hasNext()) {
					entry = iterator.next();
					writer.write(new Retort(entry.getKey(), entry.getValue()),
							HEADER);
				}

				// the actual writing

			} finally {
				writer.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
